package cn.zyb.basic.controller;

import cn.zyb.basic.dto.SmsDto;
import cn.zyb.basic.exception.BusinessException;
import cn.zyb.basic.service.IVerifyService;
import cn.zyb.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

  @Autowired
  private IVerifyService verifyService;

  @GetMapping("/image/{key}")
  public AjaxResult imageVerifyCode(@PathVariable("key") String key) {
    try {
      String base64Str = verifyService.getImgCode(key);
      return AjaxResult.me().setResultObj(base64Str);
    } catch (BusinessException e) { //业务异常捕获
      return AjaxResult.me().setSuccess(false).setMsg("获取失败!" + e.getMessage());
    } catch (Exception e) { //500
      e.printStackTrace();
      return AjaxResult.me().setSuccess(false).setMsg("系统异常,请稍后重试!");
    }
  }

  @PostMapping("/smsCode")
  public AjaxResult smsCode(@RequestBody SmsDto smsDto) {
    try {
      verifyService.smsCode(smsDto);
      return new AjaxResult();
    } catch (BusinessException e) {
      e.printStackTrace();
      return new AjaxResult(false, e.getMessage());
    } catch (Exception e) {
      e.printStackTrace();
      return new AjaxResult(false, "系统繁忙，请稍后重试!!!");
    }
  }
}
